package cn.asiontang.xposed.url_firewall;

import android.content.Context;
import android.text.TextUtils;

import cn.asiontang.BaseConfig;


/**
 * [android - Xposed how to create a module with a GUI preferences screen - Stack Overflow](https://stackoverflow.com/questions/32555529/xposed-how-to-create-a-module-with-a-gui-preferences-screen)
 * XSharedPreferences pref = new XSharedPreferences(Main.class.getPackage().getName(), "pref_mine"); // load the preferences using Xposed (necessary to be accessible from inside the hook, SharedPreferences() won't work)
 */
public class Config extends BaseConfig
{
    private static Config mConfig;

    public Config(final Context context)
    {
        super(context);
    }

    public static Config get(final Context context)
    {
        if (mConfig == null)
            mConfig = new Config(context);
        return mConfig;
    }

    public static Config get4hook()
    {
        if (mConfig == null)
            mConfig = new Config(null);
        return mConfig;
    }

    public String getHookStringAndReplaceAfter()
    {
        return mSharedPreferences.getString("HookStringAndReplaceAfter", "");
    }

    public void setHookStringAndReplaceAfter(final CharSequence s)
    {
        mSharedPreferences.edit().putString("HookStringAndReplaceAfter", String.valueOf(s)).commit();
    }

    public String getHookStringAndReplaceBefore()
    {
        return mSharedPreferences.getString("HookStringAndReplaceBefore", "");
    }

    public void setHookStringAndReplaceBefore(final CharSequence s)
    {
        mSharedPreferences.edit().putString("HookStringAndReplaceBefore", String.valueOf(s)).commit();
    }

    public String getHookStringContainsText()
    {
        return mSharedPreferences.getString("edtHookStringContainsText", "://");
    }

    public void setHookStringContainsText(final CharSequence s)
    {
        mSharedPreferences.edit().putString("edtHookStringContainsText", String.valueOf(s)).commit();
    }

    public String getHookStringLength()
    {
        return mSharedPreferences.getString("edtHookStringLength", ">0");
    }

    public void setHookStringLength(final CharSequence s)
    {
        if (TextUtils.isEmpty(s))
            mSharedPreferences.edit().remove("edtHookStringLength").commit();
        else
            mSharedPreferences.edit().putString("edtHookStringLength", (String.valueOf(s))).commit();
    }

    public String getHookStringPackageName()
    {
        return mSharedPreferences.getString("edtHookStringPackageName", null);
    }

    public void setHookStringPackageName(final CharSequence s)
    {
        mSharedPreferences.edit().putString("edtHookStringPackageName", String.valueOf(s)).commit();
    }

    public boolean isEnable()
    {
        return mSharedPreferences.getBoolean("ckbEnable", false);
    }

    public void setEnable(final boolean isChecked)
    {
        mSharedPreferences.edit().putBoolean("ckbEnable", isChecked).commit();
    }

    public boolean isEnableHookHttpClient()
    {
        return mSharedPreferences.getBoolean("EnableHookHttpClient", false);
    }

    public void setEnableHookHttpClient(final boolean isChecked)
    {
        mSharedPreferences.edit().putBoolean("EnableHookHttpClient", isChecked).commit();
    }

    public boolean isEnableHookString()
    {
        return mSharedPreferences.getBoolean("ckbEnableHookString", false);
    }

    public void setEnableHookString(final boolean isChecked)
    {
        mSharedPreferences.edit().putBoolean("ckbEnableHookString", isChecked).commit();
    }

    public boolean isEnableHookStringAndReplace()
    {
        return mSharedPreferences.getBoolean("EnableHookStringAndReplace", false);
    }

    public void setEnableHookStringAndReplace(final boolean isChecked)
    {
        mSharedPreferences.edit().putBoolean("EnableHookStringAndReplace", isChecked).commit();
    }

    public boolean isEnableHookStringWithContains()
    {
        return mSharedPreferences.getBoolean("ckbEnableHookStringWithContains", false);
    }

    public void setEnableHookStringWithContains(final boolean isChecked)
    {
        mSharedPreferences.edit().putBoolean("ckbEnableHookStringWithContains", isChecked).commit();
    }

    public boolean isEnableHookStringWithLength()
    {
        return mSharedPreferences.getBoolean("ckbEnableHookStringWithLength", false);
    }

    public void setEnableHookStringWithLength(final boolean isChecked)
    {
        mSharedPreferences.edit().putBoolean("ckbEnableHookStringWithLength", isChecked).commit();
    }
}
